<?php

namespace App\Containers\AppSection\Pledge\Models;

use App\Containers\AppSection\Pledge\Models\Properties\Apartment;
use App\Containers\AppSection\Pledge\Models\Properties\Building;
use App\Containers\AppSection\Pledge\Models\Properties\House;
use App\Containers\AppSection\Pledge\Models\Properties\Land;
use App\Containers\AppSection\Pledge\Models\Properties\Premise;
use App\Containers\AppSection\Pledge\Observers\PropertyObserver;
use App\Containers\AppSection\Type\Models\Type;
use App\Ship\Parents\Models\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Staudenmeir\EloquentJsonRelations\HasJsonRelationships;

/**
 * Залог проекта
 *
 * @property int $id Id
 * @property string $type_key Тип помещения
 * @property string $apartment_id Квартира
 * @property string $house_id Жилой дом
 * @property string $premise_id Нежилое помещение
 * @property string $land_id Земельный участок
 * @property string $building_id Здание
 *
 * @property Apartment $apartment Квартира
 * @property House $house Жилой дом
 * @property Premise $premise Нежилое помещение
 * @property Land $land Земельный участок
 * @property Building $building Здание
 * @property Type $type Тип помещения
 */
class Property extends Model
{
    use HasJsonRelationships;

    public $timestamps = false;
    protected $table = 'pledge_properties';

    protected $fillable = [
        'type_key',
        'apartment_id',
        'house_id',
        'premise_id',
        'land_id',
        'building_id',
    ];

    /**
     * A resource key to be used in the serialized responses.
     */
    protected string $resourceKey = 'Property';

    protected static function boot()
    {
        parent::boot();
        self::observe(new PropertyObserver);
    }

    /**
     * Квартира
     * @return BelongsTo Apartment
     */
    public function apartment(): BelongsTo
    {
        return $this->belongsTo(Apartment::class, 'apartment_id', 'id');
    }

    /**
     * Жилой дом
     * @return BelongsTo House
     */
    public function house(): BelongsTo
    {
        return $this->belongsTo(House::class, 'house_id', 'id');
    }

    /**
     * Нежилое помещение
     * @return BelongsTo Premise
     */
    public function premise(): BelongsTo
    {
        return $this->belongsTo(Premise::class, 'premise_id', 'id');
    }

    /**
     * Земельный участок
     * @return BelongsTo Land
     */
    public function land(): BelongsTo
    {
        return $this->belongsTo(Land::class, 'land_id', 'id');
    }

    /**
     * Здание
     * @return BelongsTo Building
     */
    public function building(): BelongsTo
    {
        return $this->belongsTo(Building::class, 'building_id', 'id');
    }

    /**
     * Тип помещения
     * @return BelongsTo Types
     */
    public function type(): BelongsTo
    {
        return $this->belongsTo(Type::class, 'type_key', 'key');
    }
}
